'use strict';

const db = uniCloud.database()
exports.main = async (event, context) => {
	// const res = await db.collection('memorization').update({
	// 	todayStudyNum: 0,
	// 	todayNeedReviewNum: 40,
	// });
	
	  const collection = db.collection('memorization')
	  const MAX_LIMIT = 100 // 每次处理的最大数量
	  let totalUpdated = 0
	  
	  // 先获取总数
	  const countResult = await collection.count()
	  const total = countResult.total
	  const batchTimes = Math.ceil(total / MAX_LIMIT)
	  
	  for (let i = 0; i < batchTimes; i++) {
	    const { data } = await collection.skip(i * MAX_LIMIT)
	      .limit(MAX_LIMIT)
	      .field({ _id: true })
	      .get()
	    
	    const updatePromises = data.map(item => {
	      return collection.doc(item._id).update({
			todayStudyNum: 0,
			todayNeedReviewNum: 40,
	      })
	    })
	    
	    await Promise.all(updatePromises)
	    totalUpdated += data.length
	  }
	  
	  return {
	    success: true,
	    totalUpdated: totalUpdated
	  }
};
